﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Tradex.Market;
using Tradex.Messages;

namespace Tradex.Market {

    /// <summary>
    /// This class represents a dynamic snapstot o the activty related to a specific instrument.
    /// This is basically a consolidation of market mode, Trade, bid and ask.
    /// </summary>
    public sealed class InstrumentActivitySnapshot : Tradex.ComponentModel.DynamicObject {

        /// <summary>
        /// Constructor for a new instance.
        /// </summary>
        public InstrumentActivitySnapshot() {
        }

        /// <inheritdoc />
        public override void ProcessMessage(Message message) {
        }

        /// <summary>
        /// Processing method to process new messages.
        /// </summary>
        /// <param name="activityUpdate"></param>
        public void Process(Tradex.Messages.InstrumentActivityUpdateMessage activityUpdate) {
            if (activityUpdate == null) {
                return;
            }
            MarketUpdate update = activityUpdate.Activity;
            if (update == null) {
                return;
            }
            switch (update.MarketUpdateType) {
                case MarketUpdateType.Trade:
                    Trade = update;
                    break;
                case MarketUpdateType.BestAsk:
                    BestAsk = update;
                    break;
                case MarketUpdateType.BestBid:
                    BestBid = update;
                    break;

                case MarketUpdateType.Open:
                    Open = update;
                    break;
                case MarketUpdateType.High:
                    High = update;
                    break;
                case MarketUpdateType.Low:
                    Low = update;
                    break;
                case MarketUpdateType.Closing:
                    Closing = update;
                    break;
                case MarketUpdateType.Close:
                    Close = update;
                    break;

                case MarketUpdateType.Mode:
                    MarketMode = update;
                    break;
                case MarketUpdateType.Opening:
                    Opening = update;
                    break;
                case MarketUpdateType.Settlement:
                    Settlement = update;
                    break;
                case MarketUpdateType.Volume:
                    Volume = update;
                    break;

            }
        }

        /// <summary>
        /// This is the current / last market mode that was set.
        /// </summary>
        public MarketUpdate MarketMode {
            get {
                return _MarketMode;
            }
            private set {
                lock (this) {
                    OnPropertyChanged("MarketModes", ref _MarketMode, value);
                }
            }
        }
        MarketUpdate _MarketMode;

        /// <summary>
        /// This is the current best bid information.
        /// </summary>
        public MarketUpdate BestBid {
            get {
                return _BestBid;
            }
            private set {
                lock (this) {
                    OnPropertyChanged("BestBid", ref _BestBid, value);
                }
            }
        }
        MarketUpdate _BestBid;

        /// <summary>
        /// This is the current best ask information.
        /// </summary>
        public MarketUpdate BestAsk {
            get {
                return _BestAsk;
            }
            private set {
                lock (this) {
                    OnPropertyChanged("BestAsk", ref _BestAsk, value);
                }
            }
        }
        MarketUpdate _BestAsk;

        /// <summary>
        /// This is the last trade information.
        /// </summary>
        public MarketUpdate Trade {
            get {
                return _Trade;
            }
            private set {
                lock (this) {
                    OnPropertyChanged("Trade", ref _Trade, value);
                }
            }
        }
        MarketUpdate _Trade;

        /// <summary>
        /// This is the opening price information, if such information was transmitted.
        /// </summary>
        public MarketUpdate Opening {
            get {
                return _Opening;
            }
            private set {
                lock (this) {
                    OnPropertyChanged("Opening", ref _Opening, value);
                }
            }
        }
        MarketUpdate _Opening;

        /// <summary>
        /// Gets the last open defined.
        /// </summary>
        public MarketUpdate Open {
            get {
                return _Open;
            }
            private set {
                lock (this) {
                    OnPropertyChanged("Open", ref _Open, value);
                }
            }
        }
        MarketUpdate _Open;

        /// <summary>
        /// Gets the closing of the last session.
        /// </summary>
        public MarketUpdate Closing {
            get {
                return _Closing;
            }
            private set {
                lock (this) {
                    OnPropertyChanged("Closing", ref _Closing, value);
                }
            }
        }
        MarketUpdate _Closing;

        /// <summary>
        /// Gets the close of the last session.
        /// </summary>
        public MarketUpdate Close {
            get {
                return _Close;
            }
            private set {
                lock (this) {
                    OnPropertyChanged("Close", ref _Close, value);
                }
            }
        }
        MarketUpdate _Close;

        /// <summary>
        /// Gets the market high of the current session.
        /// </summary>
        public MarketUpdate High {
            get {
                return _High;
            }
            private set {
                lock (this) {
                    OnPropertyChanged("High", ref _High, value);
                }
            }
        }
        MarketUpdate _High;

        /// <summary>
        /// Gets the market low of the current session.
        /// </summary>
        public MarketUpdate Low {
            get {
                return _Low;
            }
            private set {
                lock (this) {
                    OnPropertyChanged("Low", ref _Low, value);
                }
            }
        }
        MarketUpdate _Low;

        /// <summary>
        /// Gets the settlement of the last session.
        /// </summary>
        public MarketUpdate Settlement {
            get {
                return _Settlement;
            }
            private set {
                lock (this) {
                    OnPropertyChanged("Settlement", ref _Settlement, value);
                }
            }
        }
        MarketUpdate _Settlement;

        /// <summary>
        /// Gets the last transmitted session volume.
        /// </summary>
        public MarketUpdate Volume {
            get {
                return _Volume;
            }
            private set {
                lock (this) {
                    OnPropertyChanged("Volume", ref _Volume, value);
                }
            }
        }
        MarketUpdate _Volume;

    }

}
